package com.xinke.module.system.controller.admin.mail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.mail.*;
import com.xinke.module.system.model.entity.mail.MailAccountDO;
import com.xinke.module.system.service.mail.MailAccountService;

@Tag(name = "管理后台 - 邮箱账号")
@RestController
@RequestMapping("/system/mail-account")
@Validated
public class MailAccountController {

    @Resource
    private MailAccountService mailAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建邮箱账号")
    @PreAuthorize("@ss.hasPermission('system:mail-account:create')")
    public Result<Long> createMailAccount(@Valid @RequestBody MailAccountSaveReqVO createReqVO) {
        return success(mailAccountService.createMailAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新邮箱账号")
    @PreAuthorize("@ss.hasPermission('system:mail-account:update')")
    public Result<Boolean> updateMailAccount(@Valid @RequestBody MailAccountSaveReqVO updateReqVO) {
        mailAccountService.updateMailAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮箱账号")
    @Parameter(name = "mailAcctId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:mail-account:delete')")
    public Result<Boolean> deleteMailAccount(@RequestParam("mailAcctId") Long mailAcctId) {
        mailAccountService.deleteMailAccount(mailAcctId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮箱账号")
    @Parameter(name = "mailAcctId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public Result<MailAccountRespVO> getMailAccount(@RequestParam("mailAcctId") Long mailAcctId) {
        MailAccountDO mailAccount = mailAccountService.getMailAccount(mailAcctId);
        return success(BeanUtils.toBean(mailAccount, MailAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮箱账号分页")
    @PreAuthorize("@ss.hasPermission('system:mail-account:query')")
    public Result<PageResult<MailAccountRespVO>> getMailAccountPage(@Valid MailAccountPageReqVO pageReqVO) {
        PageResult<MailAccountDO> pageResult = mailAccountService.getMailAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MailAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邮箱账号 Excel")
    @PreAuthorize("@ss.hasPermission('system:mail-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMailAccountExcel(@Valid MailAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MailAccountDO> list = mailAccountService.getMailAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "邮箱账号.xls", "数据", MailAccountRespVO.class,
                        BeanUtils.toBean(list, MailAccountRespVO.class));
    }

}